<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/1/14
 * Time: 13:18
 */

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 商品详情控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class ProductController extends AdminController {

    /**
     * 商品详情显示首页
     * @author NICO
     */
    public function index(){
        $name       =   I('name');
        $map['status']  =   array('egt',0);
        $map['name']    =   array('like', '%'.$name.'%');
        $map['classname']    =  array('not in','消耗品');
        $this->meta_title = '商品信息';
        
        $list = $this->lists('product', $map);
        int_to_string($list);
        
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('_list',$list);
        $this->display();
    }

    public function createProduct(){
        if ( empty($this->product) ) {
            $this->assign('product',array('name'=>null,'id'=>null,'pinyin'=>null));//排除notice信息
        }
        
        $classList = M("classify")->where(array('status'=>1))->select();
        $data['classList'] = $classList;
        $this->data = $data;
        
        $this->meta_title = '新增商品';
        $this->display('editproduct');
    }
    public function editproduct(){
        $product = M('product')->find( (int)$_GET['id'] );
        $this->assign('product',json_encode($product));
        $this->assign('id',$product["id"]);
        $classname = $product['classname'];
        $this->assign('_classname',$classname);
        
        $classList = M("classify")->where(array('status'=>1))->select();
        $data['classList'] = $classList;
        $this->data = $data;
        
        $this->meta_title = '编辑商品';
        $this->display();
    }
    public function applypurchase(){
        if(IS_POST){
            $data = I();
            $data["createdAt"] = time();
            $data["updatedAt"] = time();
            $data["status"] = 1;
            $data["admin_uid"] = UID;
            if(count(M('Adapter_pre_purchase')->where(array('pid'=>I('pid'),'status'=>1))->select())>0){
                $this->error("该商品已在待采购列表中",U("Product/index"));
            }
            if(M('Adapter_pre_purchase')->add($data)){
                $this->success("申请成功",U("Product/index"));
            }else{
                $this->error("申请失败",U("Product/index"));
            }
        }else{
            $product = M('product')->find( (int)$_GET['id'] );
            $this->product = $product;
            $this->meta_title = '申请采购';
            $this->display();
        }

    }
    public function changeStatus($method=null){
        if ( empty($_REQUEST['id']) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'forbid':
                $this->forbid('product');
                break;
            case 'resume':
                $this->resume('product');
                break;
            case 'delete':
                $this->delete('product');
                break;
            default:
                $this->error($method.'参数非法');
        }
    }
    public function writeProduct(){
        $product = M('product');
        $data = $product->create();
        if($data){
            if(empty($data['id'])){
                $data['createdAt'] = NOW_TIME ;
                $data['createdUser'] = UID ;
                $data['updatedAt'] = NOW_TIME ;
                $data['updatedUser'] = UID ;
                $r = $product->add($data);
            }else{
                $data['updatedAt'] = NOW_TIME ;
                $data['updatedUser'] = UID ;
                $r = $product->save($data);
            }
            if($r===false){
                $this->error('操作失败'.$product->getError());
            } else{
                $this->success('操作成功!',U('index'));
            }
        }else{
            $this->error('操作失败'.$product->getError());
        }
    }

    public function check_spu(){
        $this-> suucess('success');
    }

}
